package com.dmwork.service.interaction.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmwork.common.base.result.R;
import com.dmwork.service.interaction.entity.Feed;
import com.dmwork.service.interaction.entity.FeedPictureInfo;
import com.dmwork.service.interaction.entity.vo.FeedInfoVo;
import com.dmwork.service.interaction.feign.CustomerFeignService;
import com.dmwork.service.interaction.service.FeedPictureInfoService;
import com.dmwork.service.interaction.service.FeedService;
import com.vdurmont.emoji.EmojiParser;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/front/dm_work/interaction/feed")
@RestController
public class FrontFeedController {

    @Autowired
    private FeedService feedService;
    @Autowired
    private CustomerFeignService customerFeignService;
    @Autowired
    private FeedPictureInfoService feedPictureInfoService;

    @Value("${custom.feed.picture-count}")
    private Integer feedPictureCount;

    @ApiOperation("获取动态的详情信息")
    @GetMapping("get_feed_detail/{feedId}")
    public R getFeedDetail(@PathVariable String feedId) {
        //动态详情的数据实体类
        FeedInfoVo feedInfoVo = new FeedInfoVo();
        //获取动态基本信息
        Feed feed = feedService.getById(feedId);
        if (feed == null) {
            return R.ok().message("该动态不存在");
        }
        //转换Emoji
        if (!StringUtils.isEmpty(feed.getContent())) {
            feed.setContent(EmojiParser.parseToUnicode(feed.getContent()));
        }
        BeanUtils.copyProperties(feed, feedInfoVo);

        //获取动态所属用户的用户名和头像
        String customerId = feed.getCustomerId();
        R result = customerFeignService.getBaseInfo(customerId);
        if (!result.getSuccess()) {
            return R.error().message("获取失败");
        }
        String customerName = (String) result.getData().get("customerName");
        String avatar = (String) result.getData().get("avatar");
        feedInfoVo.setCustomerName(customerName);
        feedInfoVo.setAvatar(avatar);

        //如果存在作品id，说明该动态是投稿时产生的，则该动态不存在动态图片；若不存在作品id，说明该动态是自己编写发布的，可能有图片
        if (feed.getWorkId() == null) {
            if (feed.getPictureCount() > 0) {
                List<FeedPictureInfo> feedPictureInfoList = feedPictureInfoService.list(new QueryWrapper<FeedPictureInfo>().eq("feed_id", feedId));
                List<String> pictureUrlList = new ArrayList<>();
                for (FeedPictureInfo feedPictureInfo : feedPictureInfoList) {
                    pictureUrlList.add(feedPictureInfo.getPictureUrl());
                }
                feedInfoVo.setPictureUrlList(pictureUrlList);
            }
        }
        return R.ok().data("item", feedInfoVo);
    }

    @ApiOperation("获取指定用户的动态列表")
    @GetMapping("get_customer_feed_list/{customerId}/{page}/{limit}")
    public R getCustomerFeedList(@PathVariable String customerId, @PathVariable Integer page, @PathVariable Integer limit) {
        //获取当前用户动态的基本信息
        Page<Feed> feedPage = feedService.getCustomerFeedList(customerId, page, limit);
        if (feedPage.getTotal() == 0) {
            return R.ok().message("暂无数据").data("total", 0);
        }
        //获取当前用户的用户名和头像
        R result = customerFeignService.getBaseInfo(customerId);
        if (!result.getSuccess()) {
            return R.error().message("获取失败");
        }
        String customerName = (String) result.getData().get("customerName");
        String avatar = (String) result.getData().get("avatar");

        //获取当前动态列表中动态用到的所有图片信息
        List<String> feedIdList = new ArrayList<>();
        for (Feed feed : feedPage.getRecords()) {
            feedIdList.add(feed.getId());
        }
        List<FeedPictureInfo> feedPictureInfoList = feedPictureInfoService.list(new QueryWrapper<FeedPictureInfo>().in("feed_id", feedIdList));
        //数据组装
        List<FeedInfoVo> feedInfoVoList = new ArrayList<>();
        for (Feed feed : feedPage.getRecords()) {
            FeedInfoVo feedInfoVo = new FeedInfoVo();
            //转换Emoji
            if (!StringUtils.isEmpty(feed.getContent())) {
                feed.setContent(EmojiParser.parseToUnicode(feed.getContent()));
            }
            BeanUtils.copyProperties(feed, feedInfoVo);
            feedInfoVo.setCustomerName(customerName);
            feedInfoVo.setAvatar(avatar);
            List<String> feedPictureUrlList = new ArrayList<>();
            if (feed.getPictureCount() > 0) {
                int count = 0;
                for (FeedPictureInfo feedPictureInfo : feedPictureInfoList) {
                    if (count < feedPictureCount && feed.getId().equals(feedPictureInfo.getFeedId())) {
                        feedPictureUrlList.add(feedPictureInfo.getPictureUrl());
                        count++;
                    } else if (count >= feedPictureCount) {
                        break;
                    }
                }
                feedInfoVo.setPictureUrlList(feedPictureUrlList);
            }
            feedInfoVoList.add(feedInfoVo);
        }
        return R.ok().data("total", feedPage.getTotal()).data("rows", feedInfoVoList);
    }

}
